﻿$(document).ready(function () {
    var bPwdChanged = false;//密码是否有改动
    var bAddNewUser = false;//是否新增用户
    //分页数据
    var mypage = new SmPageData("base_USER", 10);
    //用户
    var myUser = new SmObjectData("base_USER");
    var curuser = new SmUser();
    curuser.GetCurrentUser();

    $('#flexigridData').datagrid({
        iconCls: 'icon-site',
        width: 'auto',
        height: '450',
        nowrap: true,
        striped: true,
        collapsible: true,
        sortName: 'LOGIN_NAME',
        sortOrder: 'desc',
        idField: 'USER_ID',
        singleSelect: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        columns: [[
            { field: 'LOGIN_NAME', title: '用户名', width: 140},
           // { field: 'NAME', title: '真实姓名', width: 100 },
            { field: 'LEVELES', title: '用户类型', width: 140,hidden:true },
            {
                field: 'DEPT_ID', title: '隶属部门', width: 220,
                formatter: function (value, row, index) {
                        var dept = new SmObjectData("BASE_DEPT");
                        dept.Init("DEPT_ID", value);
                        return dept.GetValue("DEPT_NAME");
                }
            },
//            {
//                field: 'USER_ID', title: '隶属角色', width: 100,
//                formatter: function (value, row, index) {
//                    var role = new SmDataTable();
//                    var rolenames = "";
//                    role.Init("select ROLE_NAME from base_role where ROLE_ID in (select role_id from base_user_role where user_id='" + value + "')");
//                    if (role.RecordCount > 0) {
//                        for (var i = 0; i < role.RecordCount; i++) {
//                            rolenames += role.Data.Rows[i][0];
//                        }
//                    }
//                    return rolenames;
//                }
//            },
             {
                field: 'SEX', title: '性别', width: 60,
                formatter: function (value, row, index) {
                    if (value == "0") {
                        return "男";
                    }
                    if (value == "1") {
                        return "女";
                    }else {
                        return "保密";
                    }
                }
            },
            { field: 'PHONE', title: '手机号码', width: 100 },
            { field: 'EMAIL', title: '电子邮箱', width: 160 },
            { field: 'POSITION', title: '职务', width: 160 },
            { field: 'LAST_LOGIN', title: '上次登录时间', width: 120 }
           
        ]],
        toolbar: [{
            text: '新建',
            iconCls: 'icon-add',
            handler: function () {
                bAddNewUser = true;
                myUser.Init("USER_ID", "");
                GetValues();
                $("#userdlg").dialog("open");
            }
        },
        {
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                bAddNewUser = false;
                var node = $('#flexigridData').datagrid('getSelected');
                if (node == null) {
                    $.messager.alert('提示', '请选择一行进行修改。');
                } else {
                    myUser.Init("USER_ID", node.USER_ID);
                    GetValues();
                }
                $("#userdlg").dialog("open");
            }
        },
        {
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                var node = $('#flexigridData').datagrid('getSelected');
                if (node == null) {
                    $.messager.alert('提示', '请选择一行进行修改。');
                } else {
                    $.messager.confirm('警告', '确认要删除选中的用户吗?', function (r) {
                        if (r) {
                            GetSmJSON(CLDJServiceUrl + "/SmwsFrame/DeleteUser", "USER_ID=" + node.USER_ID, function (d) {
                                //删除用户，删除用户权限
                                reload();
                            });
                        }
                    });

                }
            }
        }
        ]
    });
    //用户角色改变后,修改隶属部门下拉框
    $('#roletree').combobox({
        onSelect: function SetDeptComb() {
            $('#rowdept').css("display", "table-row");
            $('#rowdept2').css("display", "none");
            var deptid = myUser.GetValue("DEPT_ID");
            var dept = new SmDataTable();
            dept.Init("select DEPT_ID,DEPT_NAME from BASE_DEPT");
            $('#delpttree').combobox("loadData", dept.GetEasyuiComboBoxData());
            $('#delpttree').combobox("setValue", deptid);
        
        }
    });
    //部门下拉框
    function SetDeptComb() {
        var seltext = $('#roletree').combobox("getText");
       
            $('#rowdept').css("display", "table-row");
            $('#rowdept2').css("display", "none");
            var deptid = myUser.GetValue("DEPT_ID");
            var dept = new SmDataTable();
            dept.Init("select DEPT_ID,DEPT_NAME from BASE_DEPT");
            $('#delpttree').combobox("loadData", dept.GetEasyuiComboBoxData());
            $('#delpttree').combobox("setValue", deptid);
        
    }
    //角色下拉框
    function SetRoleComb() {
        var u_r = new SmObjectData("BASE_USER_ROLE");
        u_r.GetFirstSmObject("USER_ID='" + myUser.GetValue("USER_ID") + "'");
        var roleid = u_r.GetValue("ROLE_ID");
        var role = new SmDataTable();
        var sql = "select ROLE_ID,ROLE_NAME from BASE_ROLE where ROLE_NAME<>'超级管理员'";
        if (myUser.GetValue("LOGIN_NAME") == "admin") {
            sql = "select ROLE_ID,ROLE_NAME from BASE_ROLE";
        }
        role.Init(sql);
        $('#roletree').combobox("loadData", role.GetEasyuiComboBoxData());
        $('#roletree').combobox("setValue", roleid);
    }
    
    reload();

    function refreshMyPage() {
        $("label#lblPageIndex").html(mypage.PageIndex);
        $("label#lblPageSize").html(mypage.PageSize);
        $("label#lblPageCount").html(mypage.PageCount);
        $("label#lblRecordCount").html(mypage.RecordCount);
        $('#flexigridData').datagrid({ pageNumber: mypage.PageIndex, pagination: true, pageList: [10] });
        $('#flexigridData').datagrid("loadData", mypage.GetEasyuiGridData());
        $(".datagrid-pager").hide();

    }

    function reload()
    {
        var sql = "";
        var level=curuser.LERVER;
        var deptid = curuser.DEPT_ID;
        if (level) {
            if (level == "1") {
            	//超级管理员能管理所有用户
            }
            else if(level == "2") {
            	//分中心管理员只能管理本部门用户
                sql = "DEPT_ID like '" + deptid + "%'";
            }
            else if (level == "3") {
            	//CA用户只能管理自己
                sql = "USER_ID like '" + curuser.USER_ID + "'";
            }
        }
        mypage.Init("USER_ID", "USER_ID,LOGIN_NAME,PASSWORD,DEPT_ID,SEX,LEVELES,PHONE,POSITION,EMAIL,LAST_LOGIN", sql, "USER_ID", 0);
        refreshMyPage();
    }

    $("#btnPageDown").click(function () {
        mypage.PageDown();
        refreshMyPage();
    });
    $("#btnPageUp").click(function () {
        mypage.PageUp();
        refreshMyPage();
    });
    $("#btnFirst").click(function () {
        mypage.First();
        refreshMyPage();
    });
    $("#btnLast").click(function () {
        mypage.Last();
        refreshMyPage();
    });
    //$("#Pwd1").onchange(function () {
    //    bPwdChanged = true;
    //});
    function pwdChanged() {
        bPwdChanged = true;
    }
    function GetValues() {
        InitTextOnDivFromDB("userdlg");
        //下拉框
        SetRoleComb();
        SetDeptComb();
    }

    $("#btnSave").click(function () {
        var checkrst = CheckUserInfo();
        if (checkrst == "") {
            SaveTextOnDivToDB("userdlg");
            reload();
            $("#userdlg").dialog("close");
        }
        else {
            $.messager.alert('提示', checkrst);
        }
    });

    $("#btnCance").click(function () {
        $("#userdlg").dialog("close");
    });
    //检查用户信息必填项
    function CheckUserInfo() {
        var r = "";
        if ($("#USER_ID").val() == "") {
            r = "用户代码必须填写！";
            return r;
        }
        else if ($("#LOGIN_NAME").val() == "") {
            r = "用户名称必须填写！";
            return r;
        }
        else if ($("#Pwd1").val() == "") {
            r = "密码必须填写！";
            return r;
        }
        else if ($('#roletree').combobox("getValue") == "") {
            r = "隶属角色必须填写！";
            return r;
        }
        //else if ($('#delpttree').combobox("getValue") == "") {
        //    r = "隶属部门必须填写！";
        //    return r;
        //}
        var seltext = $('#roletree').combobox("getText");
//        if (seltext == "储量评审机构") {
            if($('#delpttree').combobox("getValue")=="")
            {
                    r = "隶属部门必须填写！";
                    return r;
            }
//        }
//        else {
//            if ($('#delpttree2').combotree("getValue") == "") {
//                r = "隶属部门必须填写！";
//                return r;
//            }
//        }

        if ($("#Pwd1").val() != $("#Pwd2").val()) {
            r = "两次密码输入不一致！";
            return r;
        }
        //如果是新增用户，检查重复性
        if (bAddNewUser) {
            var u = new SmObjectData("BASE_USER");
            u.GetFirstSmObject("USER_ID=\\\'" + myUser.GetValue("USER_ID") + "\\\'");
            if (u.Data != null) {
                r = "该用户代码已经被注册";
                return r;
            }
        }
        return r;
    }
    //根据text的id，从对象中获取相应的值
    function InitTextOnDivFromDB(divID) {
        $("#" + divID + " input").each(function () {
            var id = $(this).attr("id");
            $(this).val(myUser.GetValue(id));
        });
        $("#Pwd1").val(myUser.GetValue("PASSWORD"));
        $("#Pwd2").val(myUser.GetValue("PASSWORD"));
        $("#yxzttree").combobox("setValue", myUser.GetValue("YXBZ"));
    }
    //保存
    function SaveTextOnDivToDB(divID) {
        $("#" + divID + " input").each(function () {
            var id = $(this).attr("id");
            if(id!=undefined){
            	myUser.SetValue(id, $(this).val());
            }          
        });
        // if (bPwdChanged) {
        myUser.SetValue("PASSWORD", $("#Pwd1").val());
        myUser.SetValue("DEPT_ID", $('#delpttree').combobox("getValue"));

        myUser.Update();
        //保存角色
//        var bsm = myUser.GetValue("USER_ID");
//        if (bsm == "") {
//            bsm = GetBSMByUserCode(myUser.GetValue("USER_ID"));
//        }
        //SaveUserRole(bsm);
    }
    //保存角色
    function SaveUserRole(bsm) {
        var roleid = $('#roletree').combobox("getValue");
        var u_r = new SmObjectData("SYS_ROLEUSER");
        u_r.GetFirstSmObject("USERID=\\\'" + bsm + "\\\'");
        u_r.SetValue("USERID", bsm);
        u_r.SetValue("ROLEID", roleid);
        u_r.Update();
    }
    //通过用户代码获取bsm
    function GetBSMByUserCode(ucode) {
        var u = new SmObjectData("BASE_USER");
        u.GetFirstSmObject("USER_ID=\\\'" + myUser.GetValue("USER_ID") + "\\\'");
        return u.GetValue("USER_ID");
    }

})